package com.example.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.model.common.dtos.PageRequestDto;
import com.example.model.common.dtos.PageResponseResult;
import com.example.model.common.dtos.ResponseResult;
import com.example.model.common.enums.AppHttpCodeEnum;
import com.example.model.wemedia.pojos.WmMaterial;
import com.example.model.wemedia.pojos.WmNews;
import com.example.model.wemedia.pojos.WmNewsMaterial;
import com.example.wemedia.mapper.WmMaterialMapper;
import com.example.wemedia.mapper.WmNewsMaterialMapper;
import com.example.wemedia.service.WmMaterialService;
import com.example.wemedia.service.WmNewsService;
import com.heima.file.service.FileStorageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private WmMaterialMapper wmMaterialMapper;


    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;

    /*
    图片素材上传
     */
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile, String id) {
        if(multipartFile == null || multipartFile.getSize() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        String fileName = UUID.randomUUID().toString().replace("-", "");
        String originalFileName = multipartFile.getOriginalFilename();
        String postfix = originalFileName.substring(originalFileName.lastIndexOf("."));
        String fileId = null;



        try {
            fileId = fileStorageService.uploadImgFile("",fileName+postfix, multipartFile.getInputStream());
            log.info("上传图片 {} 成功",fileId);
        } catch (IOException e) {
            log.info("上传图片失败");
            throw new RuntimeException(e);
        }

        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(Integer.parseInt(id));
        wmMaterial.setUrl(fileId);
        wmMaterial.setIsCollection((short)0);
        wmMaterial.setType((short)0);
        wmMaterial.setCreatedTime(new Date());
        wmMaterialMapper.insert(wmMaterial);
        log.info("插入数据成功");

        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult findList(PageRequestDto pageRequestDto,String userId) {
        pageRequestDto.checkParam();
        IPage page = new Page(pageRequestDto.getPage(),pageRequestDto.getSize());
        LambdaQueryWrapper<WmMaterial> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(WmMaterial::getUserId,userId);
        lambdaQueryWrapper.orderByDesc(WmMaterial::getCreatedTime);

        page = page(page,lambdaQueryWrapper);
        ResponseResult responseResult = new PageResponseResult(pageRequestDto.getPage(),pageRequestDto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult deleteOne(String id, String userId) {
        LambdaQueryWrapper<WmNewsMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmNewsMaterial::getMaterialId,id);
        List<WmNewsMaterial> wmNewsMaterials = wmNewsMaterialMapper.selectList(queryWrapper);
        if(!wmNewsMaterials.isEmpty()){
            return ResponseResult.errorResult(AppHttpCodeEnum.MATERIAL_IS_USED);
        }

        LambdaQueryWrapper<WmMaterial> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WmMaterial::getId,id)
                .eq(WmMaterial::getUserId,userId);

        wmMaterialMapper.delete(lambdaQueryWrapper);
        return ResponseResult.okResult("删除成功");
    }

}
